<template>
  <div>
    <div class="header">
      <div class="logo_div" @click="jumpToIndex">
        <img src="../assets/hospital_logo.png"  class="logo"  height="50" width="50"/><span class="title">中医管理系统</span>
      </div>
      <div style="float: right;">
        <el-button @click.native="toRecharge" style="border: none;background-color:rgb(246, 246, 246);">
          充值中心
        </el-button>
        <el-dropdown>
          <span class="el-dropdown-link">
            更多<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="showUserInfo">个人中心</el-dropdown-item>
            <el-dropdown-item @click.native="loginOut">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <el-container>
      <el-header>
        <HeaderView />
      </el-header>
      <el-main >
        <router-view />
      </el-main>
      <el-footer>
        <div class="footer-div">
          <br>
          医院地址：南宁市东葛路89-9号&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          电话（传真）：(0771)5862294<br>
          门诊咨询电话：(0771)5848546&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          急诊科：(0771)5848548-5864378<br>
        </div>
      </el-footer>
    </el-container>

    <!-- 个人信息弹框 -->
    <el-dialog :title="userInfoTitle" :visible.sync="userInfoOpen" width="500px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入医生简介" :disabled="isdisabledBtn" />
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" :disabled="isdisabledBtn" />
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="false">
          <el-input v-model="form.password" placeholder="请输入密码" :disabled="isdisabledBtn" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" :disabled="isdisabledBtn" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-if="form.sex == 0" v-model="sexDict.man" placeholder="请输入性别" :disabled="isdisabledBtn" />
          <el-input v-if="form.sex == 1" v-model="sexDict.wman" placeholder="请输入性别" :disabled="isdisabledBtn" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" :disabled="isdisabledBtn" />
        </el-form-item>
        <el-form-item label="出生年月" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入出生年月" :disabled="isdisabledBtn" />
        </el-form-item>
        <el-form-item label="是否参保" prop="isInsurance">
          <el-input v-if="form.isInsurance == 0" v-model="inSuranceDict.y" placeholder="请输入参保状态"
            :disabled="isdisabledBtn" />
          <el-input v-if="form.isInsurance == 1" v-model="inSuranceDict.n" placeholder="请输入参保状态"
            :disabled="isdisabledBtn" />
        </el-form-item>
        <el-form-item label="余额" prop="money">
          <el-input v-model="form.money" placeholder="请输入余额" :disabled="isdisabledBtn" />
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 充值弹框 -->
    <el-dialog :title="rechargeTitle" :visible.sync="rechargeOpen" width="500px">
      <el-form ref="rechargeForm" :model="rechargeForm" label-width="80px">
        <el-form-item label="充值金额" prop="money">
          <el-input v-model="rechargeForm.money" placeholder="请输入充值金额" />
        </el-form-item>
        <el-button type="primary" @click="submit">确定</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import HeaderView from './HeaderView';
import { edit } from '../api/patient';

export default {
  name: 'HomeView',
  components: { HeaderView },
  data() {
    return {
      userInfoTitle: '个人信息',
      rechargeTitle: '充值中心',
      userInfoOpen: false,
      rechargeOpen: false,
      // 个人信息表单
      form: {},
      // 性别字典
      sexDict: {
        man: '男',
        wman: '女'
      },
      // 参保状态字典
      inSuranceDict: {
        y: '已参保',
        n: '未参保'
      },
      rechargeForm: {},
      userInfo: {},
      isdisabledBtn: true,  // 是否禁止输入

    }
  },
  created() {
    // 获取用户信息
    this.userInfo = JSON.parse(localStorage.getItem('userInfo'));
    let token = localStorage.getItem('Authorization');
    if (token == undefined) {
      this.$router.push('/login');
    }
  },
  methods: {
    jumpToIndex() {
      this.$router.push('/index').catch(err => {
      console.log('输出报错',err)
})
;
    },
    // 清理本地缓存
    removeLocalStorage() {
      localStorage.removeItem('Authorization');
      localStorage.removeItem('token_expire');
      localStorage.removeItem('userInfo');
    },
    // 点击退出登录
    loginOut() {
      this.removeLocalStorage();
      this.$router.replace('/login');
      this.$message.success('退出成功！');
    },
    // 点击显示个人信息
    showUserInfo() {
      // 获取用户信息
      this.userInfo = JSON.parse(localStorage.getItem('userInfo'));
      this.userInfoOpen = true;
      this.form = this.userInfo;
    },
    // 点击打开充值中心
    toRecharge() {
      this.rechargeOpen = true;
    },
    /** 确定充值操作 */
    submit() {
      let patient = this.userInfo;
      patient.money = Number(patient.money) + Number(this.rechargeForm.money);
      edit(patient).then(res => {
        console.log(res.data)
        if (res.data.code == 200) {
          this.$message.success('充值成功！');
          this.rechargeOpen = false;
          this.userInfo = patient;
          localStorage.setItem('userInfo', JSON.stringify(patient));
        } else {
          this.$message.error('充值失败！');
        }
      });
    }
  }
}
</script>

<style>
.logo {
  width: 100px;
  height: 100px;
  margin-left: 0px;
}

.logo_div {
  display: inline-block;
}

.header {
  text-align: left;
  background: url(../assets/logo-slo.jpg) no-repeat center;
  background-size: 600px;
}

.title {
  font-size: 30px;
}

.el-footer {
  clear: both;  
    display: block; 
    height: 200px; 
    text-align: center;      
    margin: 0px auto;  
    position: absolute;  
    bottom: 0;  
    width: 100%;
    background: url(../assets/floor.jpg) no-repeat center;
    background-size: 100%;
}
.span{
  text-align: center;
  left: 500px;
}
</style>